# -*- coding: utf-8 -*-
"""
Created on Wed May 20 01:05:18 2020

@author: HP
"""

import os.path
import pandas as pd
from garp import garp_find_e
from PermTester import PermTester

df = pd.read_excel('FismanKM-giving2person.xls')
#Make prices
df['p_Hold'] = 100. / df['Max-Hold']
df['p_Pass'] = 100. / df['Max-Pass']

#If the results file doesn't exist then create it.
file_results = 'FismanKM-giving2person-perm.xlsx'

columns = [ 'panelist', 'periods', 'avg pass share', 
           'e-score', 'p-value']

if not os.path.isfile(file_results):
    df_results = pd.DataFrame( columns = columns )
    df_results.to_excel(file_results)

df_results = pd.read_excel(file_results,index_col = 0)

ids = list( set( df['ID'] ))

for panelistid in ids:
    #Make sure we have not processed already.    
    if not any( df_results['panelist'] == panelistid ):
        print('panelist {}'.format(panelistid))
        
        df_cur = df[ df['ID'] == panelistid ].copy()
        df_cur['pass share'] = ( df_cur['Pass'] * df_cur['p_Pass'] ) / \
           ( df_cur['Hold'] * df_cur['p_Hold'] + df_cur['Pass'] * df_cur['p_Pass'] )
        C = df_cur[ ['Hold','Pass'] ].to_numpy()
        P = df_cur[ ['p_Hold','p_Pass'] ].to_numpy()
        
        avg_pass_share = df_cur['pass share'].mean()
        e_score  = garp_find_e(C,P)
        pt = PermTester(C,P)
        p_value = pt('garp','r')
        
        result_cur = [ panelistid, C.shape[0], avg_pass_share, e_score, p_value ]
        result_cur_dict = dict( zip(columns,result_cur) )
        df_result_cur = pd.DataFrame( result_cur_dict, index = [panelistid] )
        df_results = df_results.append(df_result_cur)
        df_results.to_excel(file_results)


